package com.cd2cd.code_app;

import com.cd2cd.code_app.dispatcher.DispatcherHandle;
import com.cd2cd.code_app.util.Constants;
import com.cd2cd.code_app.util.FileUtil;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;

@Slf4j
public class App extends Application {

    public static final String GIT_HOME_URL = "http://lwlgit.gitee.io/code/";
    public static DispatcherHandle DISPATCHER_HANDLE = new DispatcherHandle();
    public static Process WEB_SERVICE;
    public static TabPane TAB_PANE;

    @Override
    public void start(final Stage stage) {

        FileUtil.hasSetting();

        // 首页菜单
        MenuBar menuBar = new MenuBar();
        initMenu(menuBar);
        BorderPane borderPane = new BorderPane();
        borderPane.setTop(menuBar);

        TAB_PANE = new TabPane();
        borderPane.setCenter(TAB_PANE);

        String url = App.class.getResource("/html/index.html").toExternalForm();
        App.addTab(url, null);

        // open host page
//        App.addTab(GIT_HOME_URL, "code-client");

        stage.setOnCloseRequest(event -> {
            System.out.print("监听到窗口关闭");
            if(WEB_SERVICE != null) {
                WEB_SERVICE.destroy();
            }
        });

        stage.setTitle("代码生成管理");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        stage.setScene(new Scene(borderPane, screenSize.width, screenSize.height));
        stage.show();

    }

    private void initMenu(MenuBar menuBar) {


        Menu menuFile = new Menu("开始");
        MenuItem home = new MenuItem("首页");
        MenuItem dbSetting = new MenuItem("系统设置");
        MenuItem exitApp = new MenuItem("退出");
        menuFile.getItems().addAll(home, dbSetting, exitApp);

        Menu menuProject = new Menu("项目管理");
        MenuItem projects = new MenuItem("项目管理");
        menuProject.getItems().addAll(projects);

        Menu menuView = new Menu("入门指南");


        EventHandler<ActionEvent> action = e -> {
            Object source = e.getSource();
            if(source == home) { // 首页

                String url = App.class.getResource("/html/index.html").toExternalForm();
                App.addTab(url, "首页");

            } else if(source == dbSetting) { // 系统设置

                String url = App.class.getResource("/html/config.html").toExternalForm();
                App.addTab(url, "系统设置");

            } else if(source == exitApp) { // 退出
                if(WEB_SERVICE != null && WEB_SERVICE.isAlive()) {
                    WEB_SERVICE.destroy();
                }
                System.exit(0);

            } else if(source == projects) { // 项目管理
                App.addTab(Constants.PROJECT_URL, "项目管理");
            }
        };

        home.setOnAction(action);
        dbSetting.setOnAction(action);
        exitApp.setOnAction(action);
        projects.setOnAction(action);

        menuBar.getMenus().addAll(menuFile, menuProject, menuView);
    }


    public static void addTab(String url, String title) {

        Tab seldTab = null;
        boolean noAdd = true;
        ObservableList<Tab> tabs = TAB_PANE.getTabs();
        for(Tab t: tabs) {
            MyTab myTab = (MyTab) t;
            String nUrl = myTab.getUrl();
            if(nUrl.equals(url)) {
                noAdd = false;
                seldTab = t;
                break;
            }
        }

        if(noAdd) {
            seldTab = new MyTab(url);
            seldTab.setText(title == null ? "加载中..." : title);
            TAB_PANE.getTabs().add(seldTab);
        }

        TAB_PANE.getSelectionModel().select(seldTab);
    }

    public static void main(String[] args) {
        launch(args);
    }
}
